package com.ruoyi.chengji.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.RedisKeys;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.pingjiao.domain.*;
import com.ruoyi.pingjiao.service.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程毕业要求Controller
 * 
 * @author zhangpeng
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/request/requirement")
public class GraduationRequirementController extends BaseController
{
    @Autowired
    private IGraduationRequirementService graduationRequirementService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IMajorService majorService;
    @Autowired
    private ICourseRequirementService courseRequirementService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询课程毕业要求列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:requirement:list')")
    @GetMapping("/list")
    public TableDataInfo list(GraduationRequirement graduationRequirement)
    {
        //判断redis是否有数据
        List<GraduationRequirement> graduationRequirementList = (List<GraduationRequirement>) redisTemplate.opsForValue().get(RedisKeys.CHENGJI_GRADUATION_KEYS);
        if (graduationRequirementList != null){
            return getDataTable(graduationRequirementList);
        }
        //不存在
        startPage();
        List<GraduationRequirement> list = graduationRequirementService.selectGraduationRequirementList(graduationRequirement);

        for (GraduationRequirement requirement : list) {
            //根据id获取子表
            List<String> courseNameList = new ArrayList<>();
            Long requirementId = requirement.getRequirementId();
            List<CourseRequirement> courseRequirementList = courseRequirementService.selectCourseRequirementByRequirementId(requirementId);
            requirement.setCourseRequirementList(courseRequirementList);
            //根据专业id获取专业名称
            Major major = majorService.selectMajorByMajorId(requirement.getMajorId());
            requirement.setMajorName(major.getName());
            for (CourseRequirement courseRequirement : courseRequirementList) {
                Course course = courseService.selectCourseByCourseId(courseRequirement.getCourseId());
                courseNameList.add(course.getCourseName());
            }
            requirement.setCourseNameList(courseNameList);
        }
        redisTemplate.opsForValue().set(RedisKeys.CHENGJI_GRADUATION_KEYS,list);
        return getDataTable(list);
    }

    /**
     * 导出课程毕业要求列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:requirement:export')")
    @Log(title = "课程毕业要求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GraduationRequirement graduationRequirement)
    {
        List<GraduationRequirement> list = graduationRequirementService.selectGraduationRequirementList(graduationRequirement);
        ExcelUtil<GraduationRequirement> util = new ExcelUtil<GraduationRequirement>(GraduationRequirement.class);
        util.exportExcel(response, list, "课程毕业要求数据");
    }

    /**
     * 获取课程毕业要求详细信息
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:requirement:query')")
    @GetMapping(value = "/{requirementId}")
    public AjaxResult getInfo(@PathVariable("requirementId") Long requirementId)
    {
        return AjaxResult.success(graduationRequirementService.selectGraduationRequirementByRequirementId(requirementId));
    }

    /**
     * 新增课程毕业要求
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:requirement:add')")
    @Log(title = "课程毕业要求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GraduationRequirement graduationRequirement)
    {
        return toAjax(graduationRequirementService.insertGraduationRequirement(graduationRequirement));
    }

    /**
     * 修改课程毕业要求
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:requirement:edit')")
    @Log(title = "课程毕业要求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GraduationRequirement graduationRequirement)
    {
        return toAjax(graduationRequirementService.updateGraduationRequirement(graduationRequirement));
    }

    /**
     * 删除课程毕业要求
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:requirement:remove')")
    @Log(title = "课程毕业要求", businessType = BusinessType.DELETE)
	@DeleteMapping("/{requirementIds}")
    public AjaxResult remove(@PathVariable Long[] requirementIds)
    {
        return toAjax(graduationRequirementService.deleteGraduationRequirementByRequirementIds(requirementIds));
    }
}
